package com.travel.server.manager.impl.command.order.data;

import com.travel.common.CommonConstant;
import com.travel.common.util.CollectionUtil;
import com.travel.common.util.CommonUtil;
import com.travel.common.util.DateUtil;
import com.travel.server.manager.dto.request.order.data.QueryChargeTargetListRequest;
import com.travel.server.manager.dto.response.order.data.QueryChargeTargetListResponse;
import com.travel.server.manager.dto.response.order.data.QueryChargeTargetListResponse.ChargeTargetItem;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.ProjectDal;
import com.travel.server.manager.impl.dal.data.UserTargetDal;
import com.travel.server.manager.model.dto.data.TargetStatisticsDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018\12\8 0008 22:22
 * @Desc:
 */
@Component
@Slf4j
public class QueryChargeTargetListCommand extends AbstractCommand<QueryChargeTargetListRequest, QueryChargeTargetListResponse> {

    @Autowired
    private UserTargetDal userTargetDal;

    @Autowired
    private ProjectDal projectDal;

    @Override
    protected void checkInput(QueryChargeTargetListRequest request) {
        checkPageInput(request);
        checkTimeInput(request);
    }

    @Override
    public QueryChargeTargetListResponse handle(QueryChargeTargetListRequest request) {
        checkInput(request);
        QueryChargeTargetListResponse response = new QueryChargeTargetListResponse();

        response.setTargetList(makeTargetList(request));
        response.setTotalNum(projectDal.selectOrderChargeCount());
        return response;
    }

    public List<ChargeTargetItem> makeTargetList(QueryChargeTargetListRequest request){
        boolean isMonth = CommonConstant.YES.equals(request.getIsMonth())?true:false;
        Date starTime = DateUtil.getDataTime(request.getStartTime());
        Date endTime = DateUtil.getDataEndTime(request.getEndTime());
        List<TargetStatisticsDto> list = userTargetDal.selectTargetStatistics(starTime, endTime, request.getOffset(), request.getLimit());
        List<ChargeTargetItem> targetList = new ArrayList<>();
        if(!CollectionUtil.isEmpty(list)){
            for(TargetStatisticsDto dto : list){
                ChargeTargetItem item = new ChargeTargetItem();
                item.setOrderCharge(dto.getName());
                item.setOrder(dto.getOrderCount() + "");
                Integer target;
                if(isMonth){
                    target = dto.getMonthTarget();
                }else{
                    target = dto.getDayTarget();
                }
                item.setTarget(target + "");
                item.setDifference((target - dto.getOrderCount()) + "");
                item.setComplete(CommonUtil.getProportion(dto.getOrderCount(), target));
                targetList.add(item);
            }
        }
        return targetList;
    }
}
